<template>
  <div class="favoritesManage">
    <div class="header">
      <div class="left" @click="back">
        <van-icon name="arrow-left" size="25" />
      </div>
      <div class="center">管理&nbsp;&nbsp;菜谱</div>
    </div>
    <div class="likeheader"></div>
    <div class="manageCollects">
      <ManageCollect v-for="item in collect" :key="item" :item="item" />
      <div class="bottom">
        <div class="line"></div>
        <div class="end">END</div>
        <div class="line"></div>
      </div>
    </div>
    <div class="likefooter"></div>
    <div class="footer">
      <div class="left">已选择{{ choosed.length || 0 }}篇菜谱</div>
      <div class="right">
        <div class="group">加入分组</div>
        <div v-if="choosed.length == 0" class="cancel" @click="cancel">
          取消收藏
        </div>
        <div
          v-else
          class="cancel"
          style="background-color: #de4a3b; color: white"
          @click="cancel"
        >
          取消收藏
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import ManageCollect from "@/components/ManageCollect.vue";
import { ref } from "vue";
import { storeToRefs } from "pinia";
import { useDouguoStore } from "@/stores/douguo";
const douguo = useDouguoStore();

const { choosed, collect } = storeToRefs(douguo);
const { cancelCollect, updataCollect } = douguo;

updataCollect();

const props = defineProps(["item"]);

const back = () => {
  history.go(-1);
};

const cancel = () => {
  // console.log("cancel");
  const arr1 = ref(JSON.parse(localStorage.getItem("collect") || "[]"));
  for (let i of choosed.value) {
    // console.log(arr1.value);
    let arr = arr1.value.filter((o: number) => o != i);
    arr1.value = arr;
    // console.log(arr.value);
  }

  localStorage.setItem("collect", JSON.stringify(arr1.value));
  cancelCollect();
};
</script>

<style lang="scss" scoped>
.favoritesManage {
  padding: 0 16px;

  .header {
    position: fixed;
    left: 0;
    top: 0;
    height: 50px;
    width: 100%;
    display: flex;
    align-items: center;
    background-color: white;

    .left {
      width: 25px;
      height: 25px;
      margin-left: 15px;
    }

    .center {
      font-size: 20px;
      font-weight: bold;
      height: 26px;
      line-height: 26px;
      margin-left: 120px;
    }
  }

  .likeheader {
    height: 50px;
  }

  .manageCollects {
    margin-top: 10px;

    .bottom {
      margin-top: 45px;
      display: flex;
      justify-content: center;
      align-items: center;
      font-size: 14px;
      color: #999;

      .line {
        width: 25px;
        height: 1px;
        background-color: #e9e9e9;
      }

      .end {
        margin: 0 10px;
        font-size: 12px;
        color: #e9e9e9;
      }
    }
  }

  .likefooter {
    height: 50px;
  }

  .footer {
    height: 50px;
    position: fixed;
    left: 0;
    bottom: 0;
    width: 100%;
    background-color: white;
    font-size: 14px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    color: #999;
    box-shadow: 0px 5px 5px 5px rgba(0, 0, 0, 0.5);

    .left {
      font-weight: bold;
      margin-left: 16px;
    }

    .right {
      margin-right: 15px;
      display: flex;
      align-items: center;

      div {
        width: 100px;
        height: 30px;
        text-align: center;
        line-height: 30px;
        border-radius: 15px;
        background-color: #f6f6f6;
      }

      .group {
        margin-right: 16px;
      }
    }
  }
}
</style>
